import { Component, Input } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { environment } from '@env/environment';
import { BasicDataService } from '../../basic-data/basic-data.service';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { AuthDictService } from '../../../system/dict/service/dict.service';
import { ShipStatService } from '../ship-stat.service';

@Component({
  selector: 'app-edit-all-piece-ton',
  templateUrl: './edit-all-piece-ton.component.html',
  styleUrls: ['./edit-all-piece-ton.component.less']
})
export class EditAllPieceTonComponent {
	@Input()
	record: any;
	form: FormGroup;

	constructor(
		private fb: FormBuilder,
		private shipStatService: ShipStatService,
		private modalRef: NzModalRef) {
		this.form = fb.group({
			id: [null],
			allPiece: [null, [Validators.required]],
			allTon: [null, [Validators.required]]
		});
	}

	ngOnInit(): void {
		if (this.record.id) {
			this.form.patchValue(this.record);
		}
	}

	saveAndClose(): any {
		if (!this.validate()) {
			return false;
		}
		const postData = {
			...this.form.value
		};
		this.shipStatService.updateShipBillStatus(postData).subscribe((res: any) => {
			this.modalRef.close(true);
		});
	}

	close() {
		this.modalRef.close();
	}

	validate() {
		for (const key in this.form.controls) {
			// eslint-disable-next-line no-prototype-builtins
			if (this.form.controls.hasOwnProperty(key)) {
				const element = this.form.controls[key];
				element.markAsDirty();
				element.updateValueAndValidity();
			}
		}
		return this.form.valid;
	}
}
